package org.lee.openoffice.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author jackielee
 * @version 1.0
 * @description StringUtil
 * @date 2021/6/3
 **/
public class StringUtil {
    private static String FORMATTER_SYMBOL = "{}";
    private static char FORMATTER_SYMBOL_LEFT = '{';
    private static char FORMATTER_SYMBOL_RIGHT = '}';

    public static List<String> split(String source, String delimiter) {
        if (source == null || source.isEmpty()) {
            return new ArrayList<>();
        }
        return Arrays.asList(source.split(delimiter));
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String trim(String str) {
        if (isEmpty(str)) {
            return "";
        }
        return str.trim();
    }

    public static boolean contains(String source, String str) {
        if (source == null || str == null) {
            return false;
        }
        return source.contains(str);
    }

    public static String Uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String getExt(String fileName) {
        int index;
        if (isEmpty(fileName) || (index = fileName.indexOf('.')) == -1) {
            return "";
        }
        return fileName.substring(index);
    }

    public static String checkObsFileKey(String fileKey) {
        if (isEmpty(fileKey)) {
            return fileKey;
        }
        fileKey = trim(fileKey);
        int sep = fileKey.indexOf('/');
        if (sep == 0) {
            return fileKey.substring(sep + 1);
        }
        return fileKey;
    }

    /**
     * 格式化{}内容字符串
     *
     * @param format 含 {} 字符串格式
     * @param args   参数
     * @return String
     */
    public static String format(String format, Object... args) {
        if (isEmpty(format) || format.indexOf(FORMATTER_SYMBOL) == -1 || args.length == 0) {
            return format;
        }
        StringBuilder sb = new StringBuilder(format.length() + args.length * 5);
        int len = format.length();
        int argIndex = 0;
        List<Object> argList = Arrays.asList(args);
        for (int i = 0; i < len; ++i) {
            char c = format.charAt(i);
            if (c == FORMATTER_SYMBOL_LEFT && i < len - 1) {
                char c2 = format.charAt(i + 1);
                if (c2 == FORMATTER_SYMBOL_RIGHT && argIndex < argList.size()) {
                    sb.append(argList.get(argIndex++));
                    i++;
                    continue;
                }
            }
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * double转整数字符串
     *
     * @param amount
     * @return
     */
    public static String double2IntString(double amount) {
        NumberFormat format = NumberFormat.getInstance();
        format.setGroupingUsed(false);
        format.setMaximumFractionDigits(0);
        return format.format(amount);
    }

    /**
     * 是否手机号
     *
     * @param str
     * @return
     */
    public static boolean isMobile(String str) {
        Pattern pattern = Pattern.compile("1\\d{10}");
        return pattern.matcher(trim(str)).matches();
    }

    /**
     * 随机生成昵称
     *
     * @param prefix     前缀
     * @param randLength 随机码长度
     * @param postfix    后缀
     * @return
     */
    public static String randNickName(String prefix, int randLength, String postfix) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(prefix);
        Random random = new Random(System.currentTimeMillis());
        for (int index = 0; index < randLength; ++index)
            stringBuilder.append(str.charAt(random.nextInt(str.length())));
        return stringBuilder.append(postfix).toString();
    }

    /**
     * 随机生成昵称
     *
     * @param prefix     前缀
     * @param randLength 随机码长度
     * @return
     */
    public static String randNickName(String prefix, int randLength) {
        return randNickName(prefix, randLength, "");
    }

    /**
     * 检测字符串，为null则返回空字符，否则去除空格
     *
     * @param str
     * @return
     */
    public static String checkString(String str) {
        return str == null ? "" : trim(str);
    }

    /**
     * 转base64
     *
     * @param str
     * @param charset
     * @return
     */
    public static String toBase64String(String str, Charset charset) {
        return Base64.getEncoder().encodeToString(str.getBytes(charset));
    }

    /**
     * 转字符串
     *
     * @param str
     * @param charset
     * @return
     */
    public static String fromBase64String(String str, Charset charset) {
        byte[] buffer = Base64.getDecoder().decode(str);
        return new String(buffer, charset);
    }

    /**
     * 转base64，UTF-8编码
     *
     * @param str
     * @return
     */
    public static String toBase64StringUTF8(String str) {
        return toBase64String(str, StandardCharsets.UTF_8);
    }

    /**
     * 转字符串，UTF-8编码
     *
     * @param str
     * @return
     */
    public static String fromBase64StringUTF8(String str) {
        return fromBase64String(str, StandardCharsets.UTF_8);
    }

}
